<%-- 
    Document   : checkLogin
    Created on : 19-gen-2015, 11.40.58
    Author     : Jov
--%>

<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<%@page import="java.io.*"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
            String user = request.getParameter("user");
            String password = request.getParameter("password");
            int logged = 0;
            
            try {
                String connectionURL = "jdbc:mysql://localhost:3306/meteocaldb";
                Connection connection = null;
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                connection = DriverManager.getConnection(connectionURL, "root", "0000");

                Statement stmt = connection.createStatement();
                ResultSet rset;

                String query = "SELECT * FROM users WHERE username = '"+user+"' AND password = '"+password+"'";
                rset = stmt.executeQuery(query);
                                    
                while (rset.next()) {
                    logged = 1;
                    session.setAttribute("user_id", rset.getInt("user_id"));
                }
                
                if(logged == 1){
                    session.setAttribute("user", user);
                    out.println("<meta http-equiv='refresh' content='0; url=../index.jsp'>");
                }else{
                    out.println("<meta http-equiv='refresh' content='0; url=../login.jsp'>");
                }

                stmt.close();
                connection.close();

            } catch (Exception ex) {
                out.println("Errore! " + ex.getMessage().toString());
            }
        %>
    </body>
</html>
